package com.slaffka.report;

import com.slaffka.action.ReportAction;
import com.slaffka.dao.LoanDAO;
import com.slaffka.dao.UserDAO;
import com.slaffka.model.Loan;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: slaffka
 * Date: 20.11.12
 * Time: 0:31
 * To change this template use File | Settings | File Templates.
 */
@WebServlet(urlPatterns = "/runReport")
public class RunReport extends HttpServlet{
    JasperPrint jasperPrint;
    //ServletOutputStream servletOutputStream;
    private List allUsers;

    @Inject
    UserDAO userDAO;

    @Inject
    LoanDAO loanDAO;

    @Inject
    ReportAction reportAction;

    private ServletOutputStream servletOutputStream;

    public void initReport() throws JRException {
        //allUsers = userDAO.getAllUsers();

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date dateStart = calendar.getTime();

        calendar.add(Calendar.MONTH, 1);
        Date dateEnd = calendar.getTime();

        System.out.println("dateStart = " + dateStart + ", dateEnd = " + dateEnd);

        List<Loan> loanList = loanDAO.getLoanInPeriod(dateStart, dateEnd);

        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(loanList);
                //new JRBeanCollectionDataSource(allUsers);

        String path = getServletContext()
                .getRealPath("\\WEB-INF\\jasperreports\\" + reportAction.getCurrentReport().getTemplate() + ".jasper");

        jasperPrint = JasperFillManager.fillReport(path, new HashMap(), beanCollectionDataSource);
        reportAction.setCurrentJasperPrint(jasperPrint);

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            initReport();
            servletOutputStream = response.getOutputStream();
            JRHtmlExporter exporter = new JRHtmlExporter();
            exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "UTF-8");

            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
            exporter.setParameter(JRHtmlExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
            exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
            exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
            exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, servletOutputStream);

            exporter.exportReport();

            response.flushBuffer();

        } catch (JRException e){
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
